﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Boggle;

namespace BoggleGUI
{
    public delegate void WordClickedHandler(object sender, StringEventArgs e);
    
    /// <summary>
    /// Interaction logic for BoggleBoardControl.xaml
    /// </summary>
    public partial class BoggleBoardControl : UserControl
    {
        public event WordClickedHandler WordClicked;
        
        /// <summary>
        /// 
        /// </summary>
        public BoggleBoardControl()
        {
            InitializeComponent();

            // register event handlers
            BoggleGrid.Clicked += OnClicked;
            BoggleGrid.AlreadyClicked += OnAlreadyClicked;
            BoggleGrid.RightClick += OnRightClick;
        }

        #region DisplayGrid Event Handlers
        /// <summary>
        /// Appends the label of the most recently clicked cell to 
        /// the current word.
        /// </summary>
        /// <param name="sender">The originating cell.</param>
        private void OnClicked(object sender, EventArgs args)
        {
            BoggleCell cell = (BoggleCell)sender;
            WordLabel.Content += cell.GetLabel();
        }

        /// <summary>
        /// If the most recently clicked cell is clicked again,
        /// its letter or letters is removed from the current word.
        /// </summary>
        private void OnAlreadyClicked(object sender, EventArgs args)
        {
            BoggleCell cell = (BoggleCell)sender;

            string cellLabel = cell.GetLabel();

            string oldLabel = WordLabel.Content.ToString();

            WordLabel.Content = oldLabel.Substring(0, oldLabel.Length - cellLabel.Length);
        }

        /// <summary>
        /// Clears the current word if the player right-clicks the grid.
        /// </summary>
        private void OnRightClick(object sender, EventArgs args)
        {
            WordLabel.Content = "";
        }
        #endregion

        /// <summary>
        /// If the player left-clicks the current word, the word is sent to the server,
        /// the board selection is cleared, and the word label is reset.
        /// </summary>
        private void WordLabel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (WordClicked != null)
            {
                WordClicked(this, new StringEventArgs(WordLabel.Content.ToString()));
            }

            BoggleGrid.ClearSelection();
            WordLabel.Content = "";
        }

        /// <summary>
        /// If the player right-clicks the current word, the word label is reset and
        /// the board selection is cleared.
        /// </summary>
        private void WordLabel_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            BoggleGrid.ClearSelection();
            WordLabel.Content = "";
        }
    }
}
